package com.hyj.serve.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huitian.mybatis.plus.entity.SuperEntity;
import com.hyj.serve.manage.configure.common.Result;
import com.hyj.serve.manage.entity.HostEntity;
import com.hyj.serve.manage.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HeYuJie
 * @date 2022/6/1
 */
@RestController
@RequestMapping("/host")
public class HostController {

    @Autowired
    private HostService hostService;

    /**
     * 查询列表分页
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public List<HostEntity> page(@RequestBody HostEntity entity) {
        entity.setPageSize(SuperEntity.NO_PAGE);
        return hostService.list(entity);
    }

    /**
     * 通过ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable("id") Integer id) {
        if (hostService.removeById(id)) {
            return Result.ok().build();
        } else {
            return Result.fail("删除失败！").build();
        }
    }

    /**
     * 通过ID更新
     * @param id
     * @param entity
     * @return
     */
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> updateById(@PathVariable("id") Integer id, @RequestBody HostEntity entity) {
        entity.setId(id);
        if (hostService.updateById(entity)) {
            return Result.ok().build();
        } else {
            return Result.fail("修改失败！").build();
        }
    }

    /**
     * 插入一条记录
     * @param entity
     * @return
     */
    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> save(@Validated @RequestBody HostEntity entity) {
        if (hostService.save(entity)) {
            return Result.ok(entity).build();
        } else {
            return Result.fail("保存失败！").build();
        }
    }

}
